package model;


public class IPAdress {

	private Subnet subnet;
	private String address = "";
	private Octet[] addressBits = new Octet[4];

	public IPAdress(String address) {
		this.address = address;
		convertAddressToOctets();

	}

	public IPAdress(String address, Subnet subnet) {
		this.address = address;
		this.subnet = subnet;
		convertAddressToOctets();

	}

	public IPAdress(Octet[] addressBits) {
		this.addressBits = addressBits;
		convertOctetsToAddress();

	}

	public IPAdress(Octet[] addressBits, Subnet subnet) {
		this.addressBits = addressBits;
		this.subnet = subnet;
		convertOctetsToAddress();

	}

	public Subnet getSubnet() {
		return subnet;
	}

	public void setSubnet(Subnet subnet) {
		this.subnet = subnet;
	}

	public Octet[] getAddressBits() {
		return addressBits;
	}

	public Octet getAddressBits(int i) {
		return addressBits[i];
	}

	public void setOctet(Octet bits, int octetNumber) {
		addressBits[octetNumber - 1] = bits;
	}

	public void setAddressBits(Octet[] addressBits) {
		this.addressBits = addressBits;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	private void convertAddressToOctets() {
		String[] splited = address.split("\\.");
		for (int i = 0; i < splited.length; i++) {
			addressBits[i] = new Octet(splited[i]);
		}
	}

	private void convertOctetsToAddress() {
		for (int i = 0; i < addressBits.length; i++) {
			address += addressBits[i].getBits() + ".";
		}
	}

}
